﻿using System;
using System.IO;
using System.Text;

namespace GeneralDFS.Common.IO
{
    public static class PathHelper
    {
        public static String Root
        {
            get { return ""; }
        }

        public static String Separater
        {
            get { return @"/"; }
        }

        public static String Combine(String folder, String fileName)
        {
            if (folder.EndsWith(@"/"))
                return folder + fileName;
            else
                return folder + Separater + fileName;
        }

        /// <summary>
        /// 将字符串路径转换成字符串数组路径
        /// </summary>
        /// <param name="path"></param>
        /// <returns>字符串数组</returns>
        public static String[] Separate(string path)
        {
            if (path == Root)
                return new string[]{Root};
            path = path.Replace("//", "/");
            path = path.Replace("\\", "/");
            path = path.Replace(@"\", "/");
            if (path[path.Length - 1] == '/' && path.Length > 2)
               path = path.Remove(path.Length - 1);
            return path.Split('/');
        }

        public static String FileName(String path)
        {
            var pathArray = Separate(path);
            return pathArray[pathArray.Length-1];
        }

        public static Boolean CheckPathVaild(string path)
        {
            //todo 检查路径是否合法
            return true;
        }

        /// <summary>
        /// 通过目录地址和文件名获得文件在GDFS的完整地址
        /// </summary>
        /// <param name="directoryPath">GDFS的目录地址</param>
        /// <param name="fileName">GDFS的文件名</param>
        /// <returns>GDFS的完整地址</returns>
        public static string GetGDFSPath(string directoryPath, string fileName)
        {
            if (directoryPath[directoryPath.Length - 1] == '/')
            {
                return directoryPath + fileName;
            }
            else
                return directoryPath + @"/" + fileName;
        }
        /// <summary>
        /// 通过目录地址和文件名获得文件在本机上的完整地址
        /// </summary>
        /// <param name="directoryPath">目录地址</param>
        /// <param name="fileName">文件地址</param>
        /// <returns>在本机的完整地址</returns>
        public static string GetLocalPath(string directoryPath, string fileName)
        {
            if (directoryPath[directoryPath.Length - 1] == '/')
            {
                return directoryPath + fileName;
            }
            else
                return directoryPath + @"/" + fileName;
        }

        public static string GetParentPath(string[] path)
        {
            return GetParentPath(PathArraryToString(path));
        }

        /// <summary>
        /// 获取节点父路径
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetParentPath(string path)
        {
            path = path.Replace("//", "/");
            path = path.Replace("\\", "/");
            path = path.Replace(@"\", "/");
            if (path == "/" || path == "")
                return path;
            if (path.LastIndexOf("/", System.StringComparison.Ordinal) == path.Length - 1)
                path = path.Substring(0, path.LastIndexOf("/", System.StringComparison.Ordinal));
            if (path.LastIndexOf("/", System.StringComparison.Ordinal) >= 0)
                return path.Substring(0, path.LastIndexOf("/", System.StringComparison.Ordinal));
            return null;
        }

        /// <summary>
        /// 将路径数组转换为String
        /// </summary>
        /// <param name="pathName"></param>
        /// <returns></returns>
        public static String PathArraryToString(String[] pathName)
        {
            var sb=new StringBuilder();
            foreach (var s in pathName)
            {
                if (s == "/" || s == "")
                    sb.Append("");
                else 
                    sb.AppendFormat("/{0}", s);
            }
            return sb.ToString();
        }
    }
}